package b2.widget.warehouse
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tech.*;
	import b2.widget.store.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class ObjectItemMessage extends Sprite
	{
		private var objectItem:ObjectItem;
		
		public function ObjectItemMessage(objectItem:ObjectItem)
		{
			super();
			this.objectItem = objectItem;
			var basicLevelColor:int = Config.basicConfig.objectBasicLevelColor[objectItem.basicLevel];			
			var quality:int = objectItem.basicLevel * 5 + objectItem.strengthenLevel;
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectItem.objectIndex];
			var equipConfig:EquipConfig;
			var roundRecContainer:Sprite = new Sprite();
			//名字
			var nameText:TextField = new TextField();
			nameText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			nameText.selectable = false;
        	nameText.autoSize = TextFieldAutoSize.LEFT;
        	var nameTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF, true);
			nameText.defaultTextFormat = nameTexFormat;
			if (objectConfig.type == 1)
			{
				equipConfig = objectConfig as EquipConfig;
				nameText.text = Config.basicConfig.equipSuitName[equipConfig.suitIndex] + "的" + objectConfig.name.toString();
			}
			else
			{
				nameText.text = objectConfig.name.toString();
			}
			
			//具体信息
			var tipText:TextField = new TextField();
			tipText.selectable = false;
        	tipText.autoSize = TextFieldAutoSize.LEFT;
        	tipText.wordWrap = true;
        	tipText.multiline = true;
        	tipText.width = 180;
        	var texFormat:TextFormat = new TextFormat("微软雅黑", 13, 0x000000, true);
			tipText.defaultTextFormat = texFormat;
			tipText.y = 20;
			
			//装备
			if (objectConfig.type == 1)
			{
				equipConfig = objectConfig as EquipConfig;
				//战力
				var fpSprite:Sprite = getFightPoint(equipConfig.fightPoint[quality]);
				fpSprite.x = 180 - fpSprite.width;
				fpSprite.y = 1;
				roundRecContainer.addChild(fpSprite);
				tipText.htmlText = "部位: " + Config.basicConfig.equipPosName[equipConfig.position];
			}
			
			if (!objectConfig.noBasicLevel)
			{
				var basicLevelName:String = Config.basicConfig.objectBasicLevel[objectItem.basicLevel];
				var strengthenLevelName:String = Config.basicConfig.objectStrengthenLevel[objectItem.strengthenLevel];
				if (!objectConfig.noStrengthenLevel)
	        		tipText.htmlText += "品质: <font color='#"+ Config.basicConfig.objectBasicLevelColor[objectItem.basicLevel].substr(2) + "'>" 
	        			+ basicLevelName + strengthenLevelName + "</font>";
	        	else
	        		tipText.htmlText += "品质: <font color='#"+ Config.basicConfig.objectBasicLevelColor[objectItem.basicLevel].substr(2) + "'>" 
	        			+ basicLevelName + "</font>";
			}
			//当前的底部位置
			var bottomY:int;
			//装备
			if (objectConfig.type == 1)
			{
				equipConfig = objectConfig as EquipConfig;
				tipText.htmlText += equipConfig.getDescription(quality);
				if (quality < 24)
				{
					//横线
					roundRecContainer.graphics.lineStyle(1, 0, 0.5);
					roundRecContainer.graphics.moveTo(0, tipText.height + 30);
					roundRecContainer.graphics.lineTo(190, tipText.height + 30);
					var nextLevelContainer:Sprite = new Sprite();
					//下一级
					var nextBasicLevel:int = (quality + 1) / 5;
					var nextStrengthenLevel:int = (quality + 1) % 5;
					var nextLevelText:TextField = new TextField();
					nextLevelText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
					nextLevelText.selectable = false;
		        	nextLevelText.autoSize = TextFieldAutoSize.LEFT;
					nextLevelText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF, true);
					nextLevelText.text = "下一级属性";
					nextLevelText.y = tipText.height + 40;
					nextLevelContainer.addChild(nextLevelText);
					fpSprite = getFightPoint(equipConfig.fightPoint[quality + 1]);
					fpSprite.x = 180 - fpSprite.width;
					fpSprite.y = nextLevelText.y;
					nextLevelContainer.addChild(fpSprite);
					//下一级效果
					var nextTipText:TextField = new TextField();
					nextTipText.selectable = false;
		        	nextTipText.autoSize = TextFieldAutoSize.LEFT;
		        	nextTipText.width = 180;
		        	nextTipText.wordWrap = true;
					nextTipText.multiline = true;
					nextTipText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000, true);
					nextTipText.htmlText = "品质: <font color='#"+ Config.basicConfig.objectBasicLevelColor[nextBasicLevel].substr(2) + "'>" + 
						Config.basicConfig.objectBasicLevel[nextBasicLevel] + Config.basicConfig.objectStrengthenLevel[nextStrengthenLevel] + "</font>";
					nextTipText.htmlText += equipConfig.getDescription(quality + 1);
					nextTipText.y = nextLevelText.y + 20;
					nextLevelContainer.addChild(nextTipText);
					nextLevelContainer.x = 10;
					roundRecContainer.addChild(nextLevelContainer);
					bottomY = nextTipText.y + nextTipText.height;
				}
				else
				{
					bottomY = tipText.y + tipText.height;
				}
			}
			else
			{
				tipText.htmlText += "作用: " + objectConfig.description;
				bottomY = tipText.y + tipText.height;	
			}
			//仓库中的物品才可以操作
			if (objectItem.location == 0 && objectConfig.showInStore)
			{ 
				//横线
				roundRecContainer.graphics.lineStyle(1, 0, 0.5);
				roundRecContainer.graphics.moveTo(0, bottomY + 10);
				roundRecContainer.graphics.lineTo(190, bottomY + 10);
				//售出
				var sellText:TextField = new TextField();
				sellText.selectable = false;
	        	sellText.autoSize = TextFieldAutoSize.LEFT;
				sellText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x0000FF, true, null, true);
				sellText.text = "售出";
				sellText.x = 10;
				sellText.y = bottomY + 20;
				sellText.addEventListener(MouseEvent.MOUSE_OVER, onTextMouseOver);
				sellText.addEventListener(MouseEvent.MOUSE_OUT, onTextMouseOut);
				sellText.addEventListener(MouseEvent.CLICK, onSellTextClick);
				roundRecContainer.addChild(sellText);
			}
			roundRecContainer.addChild(nameText);
			roundRecContainer.addChild(tipText);
			var roundRec:RoundRec = new RoundRec(210, roundRecContainer.height + 30, 0.8, BFont.roundRecColor);
			addChild(roundRec);
			roundRecContainer.x = 10;
			roundRecContainer.y = 10;
			addChild(roundRecContainer);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function getFightPoint(fp:int):Sprite
		{
			var fpContainer:Sprite = new Sprite();
			//战力图标
			var CampIcon:Class = ResourceHelper.getInstance().getClassByName("CampIcon");
			var fpIcon:Sprite = new CampIcon();
			fpIcon.width = 20;
			fpIcon.height = 20;
			fpContainer.addChild(fpIcon);
			//坦克战力
			var fightPoint:TextField = new TextField();
			fightPoint.selectable = false;
        	fightPoint.autoSize = TextFieldAutoSize.LEFT;
			fightPoint.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			fightPoint.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			fightPoint.text = fp.toString();
			fightPoint.x = 25;
			fpContainer.addChild(fightPoint);
			return fpContainer;
		}
		
		private function onTextMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
		}
		
		private function onTextMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
		}
		
		private function onSellTextClick(event:MouseEvent):void
		{
			if (TechTower.getInstance().parent != null)
			{
	        	TechTower.getInstance().removeAll();
	        	Warehouse.getInstance().refresh();
	  		}
			var purchaseDialog:PurchaseDialog = new PurchaseDialog(0, objectItem.objectIndex, objectItem.basicLevel, objectItem.strengthenLevel, 1);
			WidgetManager.getInstance().popOut([purchaseDialog], true, true);
			//关闭
			WidgetManager.getInstance().closeMouseOverMessage();
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			if (event.stageX < this.x || event.stageX > this.x + this.width || 
				event.stageY < this.y || event.stageY > this.y + this.height)
			{ 
				WidgetManager.getInstance().closeMouseOverMessage();
			}
		}
		
	}
}